package jvm.heap.escapeanalysis;

/**
 * @Author: ideaAdmin
 * TODO: 标量替换
 *          -Xmx100m -Xms100m -XX:+DoEscapeAnalysis -XX:+PrintGC -XX:-EliminateAllocations
 *                              开启栈上分配                          标量替换
 * Date: 2022/2/22:11:07
 * Description:
 */

public class ScalarReplace {
    public  static class Test{
        String name;

        public Test(String name) {
            this.name = name;
        }
    }

    public static class User{
        public int id;
        public String  name;
        Test test;

        public User(int id, String name) {
            this.id = id;
            this.name = name;
        }

        public User(int id, String name, Test test) {
            this.id = id;
            this.name = name;
            this.test = test;
        }

        public Test getTest() {
            return test;
        }

        public void setTest(Test test) {
            this.test = test;
        }

        public User() {
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    public static void alloc(){
        User user = new User();
        user.setId(5);
        user.setName("6666");
        Test test = new Test("");//栈上分配
        //如果这个对象里面还包含对象 那么不会启用标量替换
       // user.test = new Test("");
//        user.id = 5;
//        user.name = "6666";
    }

//    public static void main(String[] args) throws InterruptedException {
//        long start = System.currentTimeMillis();
//        for (int i = 0; i < 10000000; i++) {
//           alloc();
//        }
//        System.out.println("花费时间:" + (System.currentTimeMillis()-start));
//
//
//    }

    public static void main(String[] args) {
        Point point = new Point(1, 2);
        System.out.println("");
    }

    /**
        * TODO:替换上面的方法
    **/
//    public static void alloc(){
//        int x = 1;
//        int y = 2;
//        System.out.println("");
//    }
    static  class Point{
        private int x;
        private int y;

        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public void setX(int x) {
            this.x = x;
        }

        public void setY(int y) {
            this.y = y;
        }
    }
}
